package com.zizy.healthexpert.Activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.zizy.healthexpert.Adapter.listAddedDishesAdapter;
import com.zizy.healthexpert.Config.Config;
import com.zizy.healthexpert.Database.MyDatabase_DishesRecords;
import com.zizy.healthexpert.Practical.Practical;
import com.zizy.healthexpert.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class AnalyzeActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_analyze);
        //TODO
        /*getView*/
        TextView txtAnalyzeSimple = (TextView)findViewById(R.id.txtAnalyzeSimple);
        /*  Today's analyze only */
        MyDatabase_DishesRecords myDatabase_dishesRecords=new MyDatabase_DishesRecords(this);
        //list contains today's nutrition formatted
        ArrayList<Object> list = myDatabase_dishesRecords.SearchDatabase("*",String.format("Date = '%s'", Practical.getTimeFormat()));
        ArrayList<listAddedDishesAdapter.addedDishes> items = new ArrayList<listAddedDishesAdapter.addedDishes>();
        for (int i=0;i<list.size();i++){
            MyDatabase_DishesRecords.DishesRecords re=(MyDatabase_DishesRecords.DishesRecords)list.get(i);
            items.add(new listAddedDishesAdapter.addedDishes(null,0,re.getNutritionFormat()));
        }
        HashMap<Integer,Double> nutMap=RecordFoodsActivity.nutAllInOne(items);
        Set set=nutMap.entrySet();
        txtAnalyzeSimple.setText("");
        for(Iterator iterator=set.iterator();iterator.hasNext();){
            Map.Entry<Integer,Double> entry=(Map.Entry<Integer,Double>)iterator.next();
            String oldTxt=(String)txtAnalyzeSimple.getText();
            double suggestValue=Practical.getSuggestNutValue(entry.getKey());
            if (suggestValue==-1) continue;
            txtAnalyzeSimple.setText(String.format("%s   %s %s 單位/%s 單位 \r\n",oldTxt,Practical.getNutritionName(entry.getKey()),String.format("%.2f",entry.getValue()),suggestValue));
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.analyze, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
